package com.jeeplus.modules.guide.utils;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class RongYiBanOkHttpUtil {
    private static final Logger logger = LoggerFactory.getLogger(RongYiBanOkHttpUtil.class);


    public static String getData(String AppKey, String url, String jsonData) {
        String str="";
        try {
            jsonData=URLEncoder.encode(jsonData, "UTF-8");
//            System.out.println(jsonData);
            OkHttpClient  client = getUnsafeOkHttpClient();
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            RequestBody body = RequestBody.create(mediaType, "jsonData="+ jsonData);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("AppKey", AppKey)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("Cookie", "JSESSIONID=tWeqia5f3mV21e0BxAFyLT7U0vp3DGyBUH0nFaOh")
                    .build();
            Response response = client.newCall(request).execute();
            str = response.body().string();
        } catch (Exception e) {
            logger.error("蓉易办排队叫号推送接口报错 ："+e.getMessage());
            e.printStackTrace();
        }
        return str;
    }

    private static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient builder = new OkHttpClient();
            builder.setSslSocketFactory(sslSocketFactory);
            builder.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            //okhttp3.8以上版本
            //OkHttpClient.Builder builder = new OkHttpClient.Builder();
            //builder.sslSocketFactory(sslSocketFactory);
            // builder.hostnameVerifier(new HostnameVerifier() {
            //    @Override
            //    public boolean verify(String hostname, SSLSession session) {
            //        return true;
            //    }
            //});
            //OkHttpClient okHttpClient = builder.build();
            return builder;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
